package com.imooc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UsersVO;
import com.imooc.service.UserService;
import com.imooc.utils.IMoocJSONResult;
import com.imooc.utils.MD5Utils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api(value = "用户相关业务的接口", tags = { "用户相关业务的controller" })
public class UserController extends BasicController {

	@Autowired
	private UserService userService;

	@ApiOperation(value = "用户上传头像", notes = "用户上传头像的接口")
	@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String")
	@PostMapping("/uploadFace")
	public IMoocJSONResult uploadFace(String userId, @RequestParam("file") MultipartFile[] files) throws Exception {

		if (StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("用户ID不能为空");
		}
		// 头像文件保存路径
		String fileLocalPathName = "C:/imooc-dev-file";

		// 头像路径数据库的保存相对路径
		String filePathDBName = "/" + userId + "/face";

		FileOutputStream fileOutputStream = null;

		InputStream inputStream = null;

		try {
			if (files != null && files.length > 0) {

				// 图片名称
				String fileName = files[0].getOriginalFilename();

				if (StringUtils.isNotBlank(fileName)) {
					// 图片的完整路径
					String finalFacePath = fileLocalPathName + filePathDBName + "/" + fileName;

					filePathDBName += ("/" + fileName);

					File outFile = new File(finalFacePath);

					if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
						// 创建父文件夹
						outFile.getParentFile().mkdirs();

					}

					fileOutputStream = new FileOutputStream(outFile);
					inputStream = files[0].getInputStream();
					IOUtils.copy(inputStream, fileOutputStream);

				} else {
					return IMoocJSONResult.errorMsg("上传出错....");
				}

			} else {
				return IMoocJSONResult.errorMsg("上传出错....");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return IMoocJSONResult.errorMsg("上传出错....");
		} finally {
			if (fileOutputStream != null) {
				fileOutputStream.flush();
				fileOutputStream.close();
			}
		}

		Users user = new Users();
		user.setId(userId);
		user.setFaceImage(filePathDBName);
		userService.updateUserInfo(user);

		return IMoocJSONResult.ok(filePathDBName);

	}

	@ApiOperation(value = "查询用户信息", notes = "查询用户信息的接口")
	@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query")
	@PostMapping("/queryUserInfo")
	public IMoocJSONResult queryUserInfo(String userId) throws Exception {

		if (StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("用户ID不能为空");
		}
		Users userInfo = userService.queryUserInfo(userId);

		UsersVO usersVO = new UsersVO();
		BeanUtils.copyProperties(userInfo, usersVO);

		return IMoocJSONResult.ok(usersVO);

	}

}
